<?php

namespace app\index\controller;

use tests\thinkphp\library\think\logTest;
use think\Controller;
use think\Db;

/**
 * 支付回调类
 **/
class Notify extends Controller
{
    /**
     * 回调处理：
     * */
    public function notice()
    {
        $data = json_decode(json_encode(simplexml_load_string(file_get_contents("php://input"), 'SimpleXMLElement', LIBXML_NOCDATA)), true);

        //业务处理代码请自行实现
        if ($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS') {

            //接收附加数据
            $json_attach = $data['attach'];
            $attach_arr = json_decode($json_attach, true);

            //订单号
            $order_sn = $data['out_trade_no'];

            //查询订单支付状态；0：未支付；1：已支付
            $pay_time = Db::name('order')->where(['order_sn' => $order_sn])->value('pay_time');

            if ($pay_time == 0) {
                Db::startTrans();
                try {
                    $this->doServiceOrder($order_sn);

                    echo 'SUCCESS';
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                }
            }

        }
    }

    /**
     * 开通服务订单支付成功处理业务逻辑
     * @param string $order_sn 支付订单号
     * */
    public function doServiceOrder($order_sn)
    {
        $order_info = Db::name('order')->where(['order_sn' => $order_sn])->find();

        $order_id = $order_info['id'];
        $user_id = $order_info['user_id'];
        $pay_price = $order_info['pay_price'];
        $up_user_id = Db::name('user')->where(['id' => $user_id])->value('up_user_id');

        $time = time();

        Db::startTrans();
        try {
            // 1：修改订单支付时间
            Db::name('order')->where(['id' => $order_id])->setField('pay_time', $time);

            // 记录用户开通的服务

            // 查询用户是否已经开通过该服务且还没有过期
            $user_service_info = Db::name('user_service')->where(['user_id' => $user_id, 'service_id' => $order_info['service_id'], 'edit_expire' => 0, 'expire_time' => ['egt', time()]])->find();
            if (empty($user_service_info)) {
                $expire_time = $order_info['service_use_day'] ? calculateExpiryTimestamp($order_info['service_use_day']) : 0;
                $user_service_data = [
                    'user_id' => $user_id,
                    'service_id' => $order_info['service_id'],
                    'name' => $order_info['service_name'],
                    'price' => $order_info['service_price'],
                    'look_count' => $order_info['service_look_count'],
                    'expire_time' => $expire_time,
                    'create_time' => $time
                ];
                Db::name('user_service')->insert($user_service_data);


                // 2：给下单用户发送服务开通消息
                $msg_data = [
                    'user_id' => $user_id,
                    'order_id' => $order_info['id'],
                    'title' => '服务开通',
                    'content' => '恭喜，您已开通我平台【' . $order_info['service_name'] . '】，本次开通后服务到期日期为：' . date('Y-m-d', $expire_time) . '，祝您使用愉快。',
                    'type' => 2,
                    'create_time' => $time,
                ];
                Db::name('msg')->insert($msg_data);

            } else {
                // 还未过期，续费操作
                $expire_time = $user_service_info['expire_time'] + $order_info['service_use_day'] * 86400;
                Db::name('user_service')->where(['id' => $user_service_info['id']])->setField('expire_time', $expire_time);
            }

            $rate = $order_info['service_rate'];
            // 3：分佣
            if ($up_user_id > 0 && $rate > 0) {
                // 上级用户的角色
                $up_user_info = Db::name('user')->field('role_id,all_subordinate_pay_price,all_profit,profit')->where(['id' => $up_user_id])->find();

                // 所有下级用户累计消费的实付金额
                $all_subordinate_pay_price = $up_user_info['all_subordinate_pay_price'];

                // 查询角色信息和规则
                $profit = $pay_price * $rate / 100;
                $profit_data = [
                    'user_id' => $up_user_id,
                    'order_id' => $order_id,
                    'pay_price' => $pay_price,
                    'rate' => $rate,
                    'profit' => $profit, // 分佣金额
                    'desc' => "开通${order_info['service_name']}，${rate}%提成",
                    'type' => 1,
                    'status' => 1,
                    'update_time' => $time,
                    'create_time' => $time
                ];
                Db::name('profit')->insert($profit_data);

                // 4：修改上级用户的【总收益】【当前收益】【所有下级用户累计消费的实付金额】
                $up_user_data = [
                    'all_profit' => sprintf('%.2f', $up_user_info['all_profit'] + $profit),
                    'profit' => sprintf('%.2f', $up_user_info['profit'] + $profit),
                    'all_subordinate_pay_price' => sprintf('%.2f', $all_subordinate_pay_price + $pay_price)
                ];
                Db::name('user')->where(['id' => $up_user_id])->update($up_user_data);
            }

            // 4：更新user表每天可查看用户信息次数
            //            if($order_info['service_look_count'] > 0) Db::name('user')->where(['id' => $user_id])->setField('look_count', $order_info['service_look_count']);

            Db::commit();
        } catch (\Exception $e) {
            file_put_contents(RUNTIME_PATH . 'order.txt', $e->getMessage() . PHP_EOL, FILE_APPEND);
            Db::rollback();
        }
    }

    public function doMobileUser()
    {
        $str = '18209338618
18616171639
13818598106
13305655606
15526202350
17705463826
13979818186
13308083081
13568958885
13606770453
15886325196
15176350269
13534149351
18226601371
13773674679
19952190669
13549519528
13507762993
18893121366
13574004633
15941195831
16675176008
13975046863
13857962213
15312036096
13513651658
15892498078
13434089280
18996043006
19373551117
18221301629
15802008038
15368941791
15086018700
19835113624
13971199458
15575159815
15305417228
18727509580
15166139372
18271838233
18583370320
13873040089
13844018833
19525999718
19807658973
13679721513
18229230726
13603097488
13561962396
13873058566
13650275393
18569092277
18907996285
15115599240
13513939518
15965287382
18312683186
180081253667
18367614113
19325244861
13560635037
18982747477
18066163901
15260533361
13881921987
18593885230
15065226484
13961490811
13397579433
13607933583
19316708613
13799923030
13904374557
15983827059
18702877187
13451112978
13423106019
15802699526
13685200080
15756770789
13883665445
13755209861
13612533828
13007419119
13825651791
13940339249
18771313962
15145490966
18665633206
13984098888
15042528991
13607480022
17873576386
18684671808
13885929879
15591442913
13508016895
19330890568
18925072413
18665639793
13380001591
13734976982
15327431543
13543535980
15281210926
19166333789
13726555049
13321480084
13611513907
13081863353
19875383423
13985875355
13842011983
19171285635
13065741159
15282330655
18730322772
13003658819
13183136800
15945205866
15952798534
13329438735
18327845105
13568888194
13929186694
16621252980
13930100578
13073000378
13813190179
18553145722
15115107367
13282155888
18769191629
15304802377
15070488926
13777584182
15173158888
13887002781
19168900753
13915665521
13808967744
15840177797
13883248464
18295880711
15216650977
13364638198
13857002548
13861708499
19911595299
13469248062
15308225141
15308225141
18875830831
17365213789
15681140978
18738280963
18773472239
13562105265
13331522886
13281108753
13725123828
15966547251
13564946592
13739053080
13777584182
13552387897
18758497387
15216650977
15759178269
13406742979
13807358369
18022810183
15282330655
18730322772
15617138265
18506727736
13704499788
13815259041
13815259041
15563753797
18623774749
15186812250
13154537058
18661872726
13955322638
13778830073
18772209486
17611492939
15754983192
18742408678
18905152241
15045300905
13582512294
15920417176
15241170212
15142104835
13825013959
18975101988
13394152833
15273089090
16724951199
17780554835
18917287415
15277081158
15701627821
13756748097
15981461933
13396003397
13981183531
17755728432
18260386535
18050031201
13889811100
13356286867
18655941431
15235281682
15045651691
15874603367
13112288676
18016481208
13674322353
13464512367
18009061983
13134518938
13338227979
13946014299
15576028253
19941286659
15841981713
15315450623
13933010575
15759178267
18328596008
13487908290
15952668826
13335969789
15144190998
15545040929
18602437778
18917183688
13704343834
17513679266
15845426349
13158706331
15999153773
15945028828
13402012379
15688905888
18118643668
17150144777
15056209952
18249776212
15397065779
13316863897
18846086118
15260960533
13500370609
15921522609
15956687576
13008489962
18351750118
13465519717
13009932607
15382377367
13389335563
13188063378
13722214612
13937672184
13469067084
18742398203
13735418553
13124593768
15920488139
15281892229
15802111863
15524741119
15049555628
15645707666
15841325879
15141353064
18367280106
18041403111
13874798244
13101598444
13705166195
18814846888
18976084418
13705469981
18342333075
13708126089
15197546885
15804492121
18192627978
19165003456
13600991110
13614253125
15926607979
17770039713
13704380896
15038809081
18228570946
13852141477
17317785073
13659843117
15946806369
13980133332
15575786272
15074679400
18688771844
17708792345
15610041813
13561890567
15906569276
18962484629
18660275137
13573386033
13081863353
15133409990
15333643556
15902436888
13873966993
17611391494
15142736363
13894057999
18640945531
13874485667
13769162452
13591421978
13407358632
15367975969
18985895386
13604709058
18361211857
19828384502
13720606691
17531197867
18272841987
15881820228
13230318909
15796890872
17789868853
15161733616
13500762264
18240317975
15299325078
15566660799
13199323999
17868540850
18347949895
13901766476
13508453576
15754656616
15545577661
17370596799
15636183619
15846585737
18604065536
19119278102
15995110395
18910061080
15579058289
13349430756
18566273285
18843987528
15246322233
18018097845
13149592720
19806829991
13584856597
15150854361
15601096926
13856296993
13863300157
15568439861
18505004588
13945252549
13101599769
19290165003
13937316199
13606641343
13274473003
15326217071
18980224112
17625097906
13851083263
13007464298
13949269764
15704145178
15022318593
13613072012
13608793989
15041597699
13199110002
13331315809
15037527038
18558976143
13917791475
15164201999
18647632828
15319982190
13789252666
13953866047
13936299007
13095236720
15941535747
14794483377
15387472866
13103999829
18043664613
13894493993
13304911234
15004684578
18647971712
18515699638
15106026139
15360609092
13136039218
15641716287
18918327026
18818879289
15248054597
13887332168
13843987855
18565709951
15146491083
13840855612
13635982452
17717787417
15141311914
13168342098
13464512367
13110590978
13768756393
15141311914
13825013959
19561830588
13816683685
18804688957
18137272726
15247559116
19113213167
13109803607
19981904396
13084819555
13120616971
13095310271
15922234353
15848925658
13831285595
15052666396
15990185922
15893129975
13868696180
13034320836
18373383659
18990940792
13975016631
17372976831
16724684133
13137255762
13256876699
13582229452
15812888944
15889811330
13204659222
13982469206
18048771508
15843234808
13896602824
13309079093
15083716678
15014133561
18832365166
18820577849
18983824189
17862200232
18981525735
19568139629
13923808987
13338897277
13340366785
18843292888
15245099500
15833956892
13312614666
18768239033
15161515360
15815838825
13620310529
18132397776
19199861238
13790695451
15927794461
19828473860
15238894508
13216886916
18918532491
13983669469
18173060570
18154537942
13614253125
13361191366
18163148852
13972699409
15874935448
19901843927
15575929895
18982739698
13940589997
18907303969
17768093307
18660156053
15802593193
15558089080
15255396176
15133431434
18030997573
15545106738
13920907783
13901054432
18779925138
15242629964
13662293026
13940967129
13684635317
13845556853
17663336891
13967016091
18846766777
18109551618
13875978520
18100682222
17637626819
13779098595
18911488277
18686943328
15102409141
13869000807
13694526529
13233179009
13321669987
13754890115
13624196978
13199040667
15751868869
18611652833
16646734333
13704466080
13755112751
19307171529
13582391612
15041200722
15872499582
19804707936
15840849066
18617038047
18394798859
18625120798
13726971813
13904907003
15951718286
13876618788
13294781775
18152710888
13973147683
15838247302
13945803376
13130026132
13109893018
13087242909
18924466278
15169645092
15963712792
15535118501
13941342591
13281600000
13989093016
18830404158
13611619967
13593151083
13017357616
17302376094
13656068656
15147069855
13809967927
18580551225
18741587668
17858916979
15278689875
13704356097
13882629969
0415210968
17574608689
18773972569
18624388811
13224633353
18740107887
13633057022
17726955944
15577308979
17795085155
13857966038
15045119477
19302557971
13584232993
13684043207
18252789715
15947756584
13998210847
13739903378
13873256285
17666417247
13304429580
13562382347
13864226802
13934095725
18292621204
15704661193
15997186705
17317987531
13406453295
13819976412
15961159106
13841252302
13135715437
18945833222
18802522733
18600638688
13067929509
13752807448
18706095399
18717746132
15166139372
18282469860
13816557314
17340288292
15751678035
18522542068
13503720892
13603083153
15531946466
14755678769
13959737477
18571040723
18161568506
15802219036
15567123982
13919670108
13552445636
15843310828
13919303106
18799010508
15214455538
13985172299
18671075123
13684569648
18056888452
15084526963
17788088816
13939901987
19956480855
18245378485
19941286659
15145134916
13799380317
13875887462
15320073252
13563117871
15688246931
13417774660
19387983169
13871908243
13643570033
13085013736
15983355557
15538969769
13825185899
13896434895
18614798861
18832454158
18975859231
15074977425
15181052900
13362647709
17865709590
18045904503
13582095026
13605697289
13279943143
13943652188
13804319887
13568420477
13996784789
15949359990
13323326706
15391483123
17501621287
18757463659
13406810102
13193183456
18377230579
13966025855
18268143913
13184667813
18938768287
15257500277
18907985850
18140551896
13011529387
13559719533
15771198778
18943906951
13644578418
13187368896
18607749713
13802584139
15920656969
18904008611
13804516897
15584418999
13919341914
15537878808
13527765768
18531027362
15095390498
15228368619
18578677229
15273502661
13248001568
18633695556
13710954858
15155517419
15998626679
15283148863
18550888110
13706835949
15570896788
13805315337
13188070394
13987941464
16178386826
15157468533
15804703391
15169078659
15116680767
18322026239
18343673155
18344751115
13795208649
13759349285
17784499218
15848139687
18860476286
13233414945
15164611569
13789823555
18773818937
18509487735
17530990667
15204052668
15114677533
13895068891
18261669899
18028652612
18345971608
18345371925
15143157113
13972554521
13929211338
13520299488
13945636859
13817824318
13808281023
13977083476
13610219105
15122643826
15541739352
18686313277
13325392028
13931581817
13199194660
15060745999
15173442118
18704584018
13869793586
15145287288
19076184332
13852578857
13604039201
15890357397
13501812995
15223514127
13500473300
17776918017
18295209068
18900986876
13472194507
13905132449
18944517548
13986959157
18033064521
15364397379
13132522978
13577381866
18788158940
13307316922
13970262162
13818920959
15942793798
15007110521
15201754061
18168388721
15140024055
15253917268
13729508927
13391270702
15577308979
15585131111
13100771825
13847091685
15048008171
13019288967
15645693414
13821499903
18975551013
15527555685
13617195669
18246118133
13875675938
18654812286
15104405317
18698438831
13030549002
13860380543
15925125968
18076221753
13930556682
15856329468
13528559058
13644616869
13170416543
13840440639
13514319290
18174668288
18940916966
13159438284
18989557530
15248046796
18701959661
18823087819
15846349360
19122700363
18088699389
13630653136
13039788070
13430521348
13134455526
13921310473
15753899789
15820922655
13136666332
18073486298
18890407699
13947195799
13912705589
15806632740
13846506691
15645653056
13891881658
18397017677
13917786549
13916726775
13891881658
13963105669
13841955122
13166713434
13705068010
13999925770
18833156813
18262523992
13646300125
18519937259
13936392339
13888436579
18063368855
18911829962
13887006635
13111916606
13488534864
15191178341
13919982854
13553830025
15223306191
15865354192
15628905997
18895238937
15883537591
13333311924
18047080837
18843218335
19519328478
15113003368
13570128032
13942239365
18629597207
15588461281
13946352663
18876732668
18602211643
18756956999
15050609418
15602975298
18318446878
13904400773
18633832931
18017258787
19180824620
13601822782
16643028805
15204588137
13117563886
13509100055
13947184947
13207453167
15048340146
14758369728
15864219908
13937967272
15041918836
13684720849
18660972565
13113378116
15344812852
15266562976
13146691150
15815531473
13591346344
13609983841
18476027028
13252171262
13568980887
13982017356
18917101531
18183684949
13809175942
18552551108
13066509222
13038435268
15546349578
13835076660
13704379133
13231320881
19086075131
16645673433
15918008642
13987160831
1548853042
15072899193
18757680669
18845832452
13217158366
18664894510
19116461260
13079703479
15506007277
13552255119
15291001002
18178634873
13616984168
13856578889
13862736603
18845266938
15776899090
18947556187
18990683676
15278962714
13766801575
18281000035
18738879282
13451307538
18220193816
18691631727
18369186215
13275166509
13630510952
13846206789
15566867330
13593439391
15317717956
19137155726
15009672219
15840346633
13704176195
13043231291
13616399158
15052062806
13944250758
13947416868
15894446625
13891831572
13322096551
13957433456
13502094732
18635313657
15874390068
15038052136
3837278995
13994057477
13155185398
15941477383
15949117458
15884282898
13803602256
15124551493
13945906962
15817429534
13998210847
13591177790
13529431335
15953177004
18324546659
13765369149
13159724799
13755205173
13578776677
18741573368
13392531502
13795074256
15776633868
18629472734
13693359655
15570618269
18353587189
13945284996
14784548672
15675847688
18513233272
15666974348
15952798534
15850783282
18368111787
13138361368
18879369399
13643614286
15143347712
15728020787
13057817060
18987015886
15524599367
13007026228
17612249131
13012730772
15105802243
13904095099
15673722906
15621672608
13304373866
19997516613
18974918172
13029075208
15974390399
15524599367
85260360019
13894091920
13309633820
13898490796
15164541331
13655370721
18995517168
18194720809
15764678456
13733488572
15657924888
13848388400
13337571198
13973393418
13297755697
18733433026
18318883707
13991594468
18032453280
16607971343
13511138156
15338943606
18930624495
13585707110
13997124634
15244659021
13847004455
13087736960
18636600318
13821786302
18686563422
13961438332
18296981966
18933341243
18048059950
13379020242
18796745393
18751523466
13878239576
18859971889
18694998997
13654279912
13845116005
13762204838
19513065837
13284941777
18990995920
17699966785
15048250715
13514479118
17366028873
15043584567
13957142034
13470433318
15937502807
13574004633
13534882549
a0915389895
15285544339
18580181678
13944085157
13377599008
15026699661
15102460160
13263804988
16563853777
13332389933
13808484100
13294634728
15300092225
13946099486
15073006666
13032263007
18266065868
13191771898
15131561686
15041713333
15040058308
18026705761
15941909911
13700598058
16621781313
15843070788
18323684528
15511768449
15056513705
15977235858
13583378528
13845595135
13819986412
13986036085
19382131349
18673737002
18954127777
18725691798
13880279815
15626092439
15044275706
15008056665
18758828977
13701267563
13715429517
13002774463
13503020502
15925358183
13507286877
15539879069
13335795802
15248054597
18607372258
13717821160
18931763828
13078881770
19918840387
18640536261
13503299718
13841438035
13695213814
18909370175
13070807725
13820692018
16637951568
18048338516
15870109828
13609845897
13946571770
15945127513
18763122577
13084819555
13841359824
13555886735
13807527155
13555061178
13803329009
13263484897
13111238292
15968111543
13942638881
13557744913
13669930885
15915634593
18650928265
18318917247
13709971736
18508433182
13152212085
15054517070
13364309811
13832528328
13465090517
13927329128
18272157955
447472376306
13710610738
13136602114
13785328396
13696432740
18993256217
13667600885
18999161223
15885949922
15652474787
15987449999
18922768288
15826145219
13670215739
13909317577
13437334790
19974896579
15972004977
13625317178
15515808783
18868895232
15241841344
13984098888
18984962867
13896248173
13731858118
18861239586
13988671669
18634992978
13704810668
13471570755
15807404668
15252011610
13683274176
13982662997
13538454962
13817007885
13017229834
15987228573
13972579098
13847089255
15230026904
13234522685
13474069269
15940985688
18191605522
15591285862
13846052235
13358476666
17623737375
13357310251
18210124618
13945364613
15174760888
15737729553
15084662866
15047651888
15257500277
17829243419
18875842628
13372720751
13963643196
17827341579
15115017609
15696294243
18973459065
18173734196
13609324975
13952058719
15116340077
15138263636
13808429212
13102188940
15334800608
17775500394
18140420296
13293309497
13770666816
17310430029
19130869283
13903391628
13569335529
13470232138
17755197675
16680311338
18133830757
15265322021
19990285711
17776095029
19859247148
13871043961
15524895983
15925676013
15873387009
13989281154
18904078228
18069727807
13840933315
18233698620
13887421522
15536786988
15804667038
18953279365
15888665989
13657711557
17345453169
13722248110
13480794152
13274338397
13737727182
15143409063
18008813242
13063690518
14795829413
17701228279
13971781278
13513859818
15962398486
18304560200
15311434548
13087352122
13338364351
13980479079
13771441918
18504392716
15645570003
13518289533
18117445891
17771028888
13913422668
15917029378
13149563666
13590303619
13375194976
15968067718
15185157451
15272122268
13827650183
13208758798
13909218151
13407553099
13069492799
13579532395
18620501513
18204077092
18190504973
13142232687
13106020863
13142232687
13963643196
15942547077
13605816236
15580052856
15637195285
18008813242
13666458910
19126449705
15545849778
15116107583
13160066780
13057669877
17769644625
15820717877
14787328976
13031126186
15115889569
18820567382
19823487195
15268785699
13789501587
13632823409
13786871772
13409862388
13528650669
15048025546
13702275966
19921919092
17634084579
19961283308
13592666676
13557653851
15044548390
13872784858
18343914113
15681207323
19958916167
13928280841
13144034313
15599139117
15018918513
18249510182
15157838568
13875286915
13147240801
13338314186
13556777899
18273173978
15958191728
19823487195
13789501587
19921919092
18862942666
13102221228
13848580208
13998110810
13844432255
13681935307
18341958441
15902689688
18586123985
18741444186
15310396836
18566334383
13127003095
13573827889
18005302388
18942957898
19973193198
15899676035
13353588255
13942465696
18792509775
13898827575
13890929638
13390540042
13869660915
13957431902
13695179641
13473965931
18245188522
15778684102
13315113616
13304326099
15063132957
15924412758
14794525080
19921138841
13591390981
18602216881
15142812858
18775334319
18005628207
13407788566
13897195966
17775793133
18013806160
13456266660
19173081899
13956471619
13215840792
18067716676
17397269876
13298731913
15194313971
13482633244
15120295858
13303954048
15873035683
15850534912
18348585141
19903940093
13997008836
13555088800
19017510082
19533819417
13874824749
13215840792
13945786606
13838156055
18359553288
18181953492
13418998401
15238000088
15200096067
13789882900
13488196143
13845242588
18227121086
15246345843
13328141886
13630811655
13955988099
13235505160
13191686989
15029755519
15092177683
15092177683
13294967652
13857430089
17374486375
18956969510
13593151083
13593151083
15897714367
13458900671
13132687118
14758481884
15040390817
18080863979
13925738316
13845051116
13936152593
18533526377
15137037123
15101589287
13234465879
13756526281
13880500175
15313333324
13323003686
13514491616
18306912987
15949964549
13181738605
18245021332
13332499987
13470466230
13845051116
18277978889
15722921349
18970400605
15045341192
13303401989
13836958810
13589819698
18746108668
15538380961
17673252538
18322026239
18084356251
13810599194
19208705599
19236436556
13265355621
13965085778
13581956208
13472570578
13540894345
15595618750
15244623003
15109609581
13736417997
18351292279
15827834823
13807237607
13634478020
15828446441
13521074124
17804654757
13404824595
13947775058
13718623810
13692267588
17703170336
13895790767
18647145402
15029755519
18398133226
17340221225
13079811109
15152665768
19577209387
15568538926
13903134785
17685530379
13664087787
13617720692
18099315311
15843435411
13984069851
18515880113
13901485153
13599615999
18677999777
14769194949
13857643558
13943067789
13775227204
18810156514
13006332593
18631768528
13522571447
15012726686
13073817309
13945402351
15526937958
13827831028
15954523232
15847614985
13133883162
17610892598
13926551598
18396699626
13541020723
17719670513
15211479965
15252011610
18241396693
13987964995
13918031373
18670920915
13511108149
13024178388
13214242819
13679219113
14741121989
15844307557
15115137578
18756053903
18074645295
15201725677
18628070337
13595429681
18920828297
18843292902
15961323885
13918764967
13945681378
15062950089
15821980284
13034586746
15759443813
13551391618
13936006629
15842560206
18028654848
13716937351
15566817511
13828563119
15998185113
13971623926
13571849345
15113759376
18174449799
13957149450
15241237677
13835902773
15593630558
13765266767
15753894872
13601719135
13700039247
13543007164
13795839903
13944850185
13682270488
18647146940
13623672429
13387534181
13972153845
13999558308
15711305853
13591987355
13347010218
15711305853
19386953834
13347010218
13611367257
18117905195
13610853664
13845290666
15235409555
15921904677
13714296497
13875288018
13611415242
15952270290
18620005001
13867090199
13941535779
15889901501
18940101961
13969469666
15765058025
13342664510
13822818029
15120035543
13125579001
18245963356
13856553908
18290136420
13667217888
13365653268
13387728341
13644971372
15773878277
13689783193
18945702999
13515278918
13818537396
13687615666
13573573218
15338553671
13855119778
13667397033
15338553671
15940126628
18759168653
13936570968
18636122516
13667217888
18311421315
13521744500
13916998003
13847527766
15125412389
13501921431
13973356218
13261813307
13683398176
18091632023
13313683195
13262091033
18763571366
15843829781
15156354057
13867090199
13013741138
13502021868
13734897177
13363990911
13161046938
18879135116
15855433042
13847361511
13525138232
15849177133
13311251918
13595571660
19185970698
13713805019
18289577055
18173708708
15840238073
18879135116
13482178045
13873501653
18108290764
13671646618
15526870626
13139081681
13979003709
18670218059
13784021742
13975414656
13204750936
13352538881
18654734613
17739932959
18322999199
13500407257
15035840027
17695702210
13796963099
15838586087
15517965695
13907484169
13616815562
13689800003
15611828666
13948538323
18337761635
15838586087
18653029836
18680393338
15888637816
18370185819
15648006661
18867493048
19941904431
13704810668
19840111042
13482618156
15132195143
13953715229
13936407699
15973317136
18202927562
13777315550
13791161161
15827580498
13731110115
18327160115
15301329088
13083538388
13968331610
15101269626
18093199746
13908291058
15510068728
13363646398
15825375610
15945126456
15838586087
15564294768
18920383083
13399160351
18782488709
15309961319
18546020978
18171291782
17635327586
13870305278
13988938896
15636561508
13988938896
13684540902
15204630450
13135113149
17564466672
13908237427
13698162699
15158945218
18781399772
18045136337
13820992256
18808565088
13567409660
13452991315
13182186607
13694322772
15084940539
13667119425
18079004188
15904720934
18408205781
18792418697
15192485852
13351829519
18202477526
15904608942
13566787061
15602135620
15184113338
13118084967
13054245966
18924300931
13289877089
18587023250
15140015158
13940072795
15051677661
13756026909
13696122995
15275229519
13981249969
13571177961
14794525080
13205438112
13559719533
13609088363
13877554328
18745728697
13686820965
15234042538
18694271819
15846375759
15542664191
13352333712
13945204503
15695710127
18862793835
19215391433
15063017866
13804684350
18805910817
13889256565
15145115587
15542664191
15599389962
13951750922
13067911353
13757826911
18958877855
15882168634
18949074852
13978846875
13856340056
13987924108
13027143608
13558728785
13951926292
15585299590
13532473541
18843597966
15526194069
13019347977
13167147268
15019637999
18268655628
13016375803
13338227570
13968009195
13515581578
18869378276
15998381498
13846869810
13808332997
15575979286
18680507321
13319850034
13856120813
15586516361
13895009038
15254438883
15809154200
13298724881
13883337828
18580507751
15943351171
15022520406
13832528328
15806805706
13706236000
13340038353
13534261248
13918519349
18395950677
13807211811
15378847529
13875460907
13667446318
13086769088
18048330063
17397271693
18847990989
15945161717
15698877095
13676804827
13487042015
15986438307
13587836287
13738241133
17841401620
13980559864
18293665057
18362746643
13865144035
16602206299
18568363632
18942957898
15899676035
18245188522
19173081899
13482633244
15873035683
15238000088
15897714367
18322026239
13807237607
13827831028
15115137578
18074645295
15113783976
13822818029
18759168653
13873501653
18108290764
18867493048
13482618156
15132195143
18327160115
18782488709
13918519349
13676804827
13487042015
18675278549
15986438307
17841401620
13876696777';
        $mobile_list = explode("\n", $str);
        $mobile_list = array_values(a_array_unique($mobile_list));
        $mobile_list_str = implode(',', $mobile_list);
        $mobile_list_str = str_replace(["\r", "\n"], '', $mobile_list_str);

        $user_id_list = Db::name('user')->whereIn('mobile', $mobile_list_str)->column('id');

        echo "<pre>";
        print_r('长度：' . count($user_id_list) . PHP_EOL);
        print_r($user_id_list);
        exit;
        foreach ($user_id_list as $v) {
            $this->doMobileUserOrder($v, 3);
        }
    }

    public function openTest()
    {
        exit;
        $mobile_list = ['18063368855', '13787522228', '13818294239', '13984886675', '15588568419'];
        //            $mobile_list = ['13881921987', '18593885230', '13978690018', '18975101988', '13804306938', '18792509775', '13845595135'];
        //                $mobile_list = ['13782317772', '18862942666', '15333036050', '13007419119', '13116911866', '18730322772', '15061803833', '18742408678', '13778427665', '13786500631', '13433383093', '13970298566', '19121008890', '18609971513', '13067276887', '18161568508', '18075575888', '13643570033'];
        $user_id_list = Db::name('user')->whereIn('mobile', $mobile_list)->column('id');

        // 数组转字符串
        $user_id_str = implode(',', $user_id_list);

        foreach ($user_id_list as $v) {
            $this->doMobileUserOrder($v, 8);
        }


    }

    public function doMobileUserOrder($user_id, $service_id)
    {
        $service_info = Db::name('service')->where(['id' => $service_id])->find();
        $time = time();

        // 查看该用户已开通的最高等级会员
        $user_service_info = Db::name('user_service')->where(['user_id' => $user_id, 'expire_time' => ['gt', $time]])->order('price desc')->find();
        if ($service_info['price'] < $user_service_info['price'] && $service_info['use_day'] > 0) {
            file_put_contents(RUNTIME_PATH . '/无需开通用户.txt', $user_id . ' ==> 该用户已开通更高等级会员，无需再次开通' . PHP_EOL, FILE_APPEND);
        } else {
            if ($service_info) {
                // 查询是否有可用优惠券信息

                Db::startTrans();
                try{
                    // 生成待支付订单
                    $order_sn = 'SN' . date('YmdHis', $time) . mt_rand(1000, 9999);
                    $pay_price = $service_info['price'];

                    $order_info = [
                        'user_id' => $user_id,
                        'order_sn' => $order_sn,
                        'service_id' => $service_info['id'],
                        'service_name' => $service_info['name'],
                        'service_price' => $service_info['price'],
                        'service_rate' => $service_info['rate'],
                        'service_use_day' => $service_info['use_day'],
                        'service_look_count' => $service_info['look_count'],
                        'coupon_name' => '',
                        'coupon_price' => 0,
                        'pay_price' => $pay_price,
                        'from' => 2,
                        'pay_time' => $time,
                        'create_time' => $time
                    ];
                    $order_id = Db::name('order')->insertGetId($order_info);

                    $order_info['id'] = $order_id;

                    // 记录用户开通的服务

                    // 查询用户是否已经开通过该服务且还没有过期
                    $user_service_info = Db::name('user_service')->where(['user_id' => $user_id, 'service_id' => $order_info['service_id'], 'edit_expire' => 0, 'expire_time' => ['egt', time()]])->find();
                    if (empty($user_service_info)) {
                        $expire_time = $order_info['service_use_day'] ? calculateExpiryTimestamp($order_info['service_use_day']) : 0;
                        $user_service_data = [
                            'user_id' => $user_id,
                            'service_id' => $order_info['service_id'],
                            'name' => $order_info['service_name'],
                            'price' => $order_info['service_price'],
                            'look_count' => $order_info['service_look_count'],
                            'expire_time' => $expire_time,
                            'create_time' => $time
                        ];
                        Db::name('user_service')->insert($user_service_data);

                        // 2：给下单用户发送服务开通消息
                        $msg_data = [
                            'user_id' => $user_id,
                            'order_id' => $order_info['id'],
                            'title' => '服务开通',
                            'content' => '恭喜，您已开通我平台【' . $order_info['service_name'] . '】，本次开通后服务到期日期为：' . date('Y-m-d', $expire_time) . '，祝您使用愉快。',
                            'type' => 2,
                            'create_time' => $time,
                        ];
                        Db::name('msg')->insert($msg_data);

                    } else {
                        // 还未过期，续费操作
                        $expire_time = $user_service_info['expire_time'] + $order_info['service_use_day'] * 86400;
                        Db::name('user_service')->where(['id' => $user_service_info['id']])->setField('expire_time', $expire_time);
                        file_put_contents(RUNTIME_PATH . '/服务续费成功.txt', $user_id . ' ==> ' . $order_info['id'] . PHP_EOL, FILE_APPEND);
                    }

                    Db::commit();
                }catch (\Exception $e){
                    Db::rollback();
                }
            } else {
                file_put_contents(RUNTIME_PATH . '/服务授权失败.txt', $user_id . ' ==> 服务授权失败');
            }
        }
    }


    /**
     * 开通服务订单支付成功处理业务逻辑
     * @param string $order_sn 支付订单号
     * */
    public function doAdminServiceOrder($order_sn)
    {
        $order_info = Db::name('order')->where(['order_sn' => $order_sn])->find();

        $order_id = $order_info['id'];
        $user_id = $order_info['user_id'];
        $pay_price = $order_info['pay_price'];
        $up_user_id = Db::name('user')->where(['id' => $user_id])->value('up_user_id');

        $time = time();

        Db::startTrans();
        try {
            // 1：修改订单支付时间
            Db::name('order')->where(['id' => $order_id])->setField('pay_time', $time);

            // 记录用户开通的服务

            // 查询用户是否已经开通过该服务且还没有过期
            $user_service_info = Db::name('user_service')->where(['user_id' => $user_id, 'service_id' => $order_info['service_id'], 'edit_expire' => 0, 'expire_time' => ['egt', time()]])->find();
            if (empty($user_service_info)) {
                $expire_time = $order_info['service_use_day'] ? calculateExpiryTimestamp($order_info['service_use_day']) : 0;
                $user_service_data = [
                    'user_id' => $user_id,
                    'service_id' => $order_info['service_id'],
                    'name' => $order_info['service_name'],
                    'price' => $order_info['service_price'],
                    'look_count' => $order_info['service_look_count'],
                    'expire_time' => $expire_time,
                    'create_time' => $time
                ];
                Db::name('user_service')->insert($user_service_data);

                // 2：给下单用户发送服务开通消息
                $msg_data = [
                    'user_id' => $user_id,
                    'order_id' => $order_info['id'],
                    'title' => '服务开通',
                    'content' => '恭喜，您已开通我平台【' . $order_info['service_name'] . '】，本次开通后服务到期日期为：' . date('Y-m-d', $expire_time) . '，祝您使用愉快。',
                    'type' => 2,
                    'create_time' => $time,
                ];
                Db::name('msg')->insert($msg_data);

            } else {
                // 还未过期，续费操作
                $expire_time = $user_service_info['expire_time'] + $order_info['service_use_day'] * 86400;
                Db::name('user_service')->where(['id' => $user_service_info['id']])->setField('expire_time', $expire_time);
            }

            // 3：分佣
            /*$rate = $order_info['service_rate'];
            if ($up_user_id > 0 && $rate > 0) {
                // 上级用户的角色
                $up_user_info = Db::name('user')->field('role_id,all_subordinate_pay_price,all_profit,profit')->where(['id' => $up_user_id])->find();

                // 所有下级用户累计消费的实付金额
                $all_subordinate_pay_price = $up_user_info['all_subordinate_pay_price'];

                // 查询角色信息和规则
                $profit = $pay_price * $rate / 100;
                $profit_data = [
                    'user_id' => $up_user_id,
                    'order_id' => $order_id,
                    'pay_price' => $pay_price,
                    'rate' => $rate,
                    'profit' => $profit, // 分佣金额
                    'desc' => "开通${order_info['service_name']}，${rate}%提成",
                    'type' => 1,
                    'status' => 1,
                    'update_time' => $time,
                    'create_time' => $time
                ];
                Db::name('profit')->insert($profit_data);

                // 4：修改上级用户的【总收益】【当前收益】【所有下级用户累计消费的实付金额】
                $up_user_data = [
                    'all_profit' => sprintf('%.2f', $up_user_info['all_profit'] + $profit),
                    'profit' => sprintf('%.2f', $up_user_info['profit'] + $profit),
                    'all_subordinate_pay_price' => sprintf('%.2f', $all_subordinate_pay_price + $pay_price)
                ];
                Db::name('user')->where(['id' => $up_user_id])->update($up_user_data);
            }*/

            // 4：更新user表每天可查看用户信息次数
            //            if($order_info['service_look_count'] > 0) Db::name('user')->where(['id' => $user_id])->setField('look_count', $order_info['service_look_count']);

            Db::commit();
        } catch (\Exception $e) {
            file_put_contents(RUNTIME_PATH . 'order.txt', $e->getMessage() . PHP_EOL, FILE_APPEND);
            Db::rollback();
        }
    }
}